<?php
/**
 * アプリ基盤サンプルクラス<br />
 *
 * アプリ基盤のサンプル(ログイン)
 *
 * @package apamanshop
 * @author hiasa-ryousuke@systemsoft.co.jp
 * @copyright コピーライト(要確認)
 */
class Mypage_MypageController extends Apm_Controller_Base
{
	
	protected $_login_error_helper = null;
	protected $_login_news_helper = null;
	protected $_login_not_found_email_helper = null;
	protected $_login_entry_helper = null;
	protected $_login_form_helper = null;
	protected $_login_pr_helper = null;
	protected $_login_back_helper = null;
	protected $_login_attention_helper = null;
	
	/**
     * ログインアクション
     *
     * @access public
     * @return void
     */
	public function indexAction()
	{
		
		$this->_model = new Mypage_Model_Login();//モデル呼び出し
		$this->_params = $this->getParameters();//パラメータ取得
		
		$this->_login_entry_helper = $this->getApmHelper('login_entry_helper.tpl');
		$this->_login_form_helper = $this->getApmHelper('login_form_helper.tpl');
		$this->_login_news_helper = $this->getApmHelper('login_news_helper.tpl');
		$this->_login_pr_helper = $this->getApmHelper('login_pr_helper.tpl');
		$this->_login_back_helper = $this->getApmHelper('login_back_helper.tpl');
		$this->_login_attention_helper = $this->getApmHelper('login_attention_helper.tpl');
		
		$this->_data['login'] = $this->_model->strFormParam($this->_params);
		$this->_data['flag'] = $this->_model->flagSet($this->_params);
		$this->_data['param'] = $this->_model->strParamHtml($this->_params);
		$this->_data['link'] = $this->_model->getBackLinkURL($this->_params);
		
		$this->_data['login_entry_helper'] = $this->_login_entry_helper->getBlock("");
		$this->_data['login_news_helper'] = $this->_login_news_helper->getBlock($this->_data);
		$this->_data['login_form_helper'] = $this->_login_form_helper->getBlock($this->_data);
		$this->_data['login_pr_helper'] = $this->_login_pr_helper->getBlock($this->_data);
		$this->_data['login_back_helper'] = $this->_login_back_helper->getBlock($this->_data);
		$this->_data['login_attention_helper'] = $this->_login_attention_helper->getBlock("");
		
		$this->display();
	}
	
	/**
     * ログインアクション
     *
     * @access public
     * @return void
     */
	public function loginAction()
	{
		$this->getResponse()->setHeader("Cache-Control","no-cache")->setHeader("Pragma","no-cache")->sendResponse();//ブラウザキャッシュ削除
		
		
		$this->_model = new Mypage_Model_Login();//モデル呼び出し
		$this->_params = $this->getParameters();//パラメータ取得
		
		$loginData = array();
		$emailData = array();
		$stMssege = "";
		
		//ヘルパー呼び出し
		//$this->_login_error_helper = $this->getApmHelper('login_error_helper.tpl');
		//$this->_login_news_helper = $this->getApmHelper('login_news_helper.tpl');
		//$this->_login_not_found_email_helper = $this->getApmHelper('login_not_found_email_helper.tpl');
		//$this->_login_form_helper = $this->getApmHelper('login_form_helper.tpl');
		
		$this->_data['login'] = array();
		//$this->_data['login_error_helper'] = "";
		//$this->_data['login_not_found_email_helper'] = "";
		//$this->_data = $this->_model->strFormParam($this->_params);
		
		//$this->_data['login_news_helper'] = $this->_login_news_helper->getBlock($this->_data);
		//$this->_data['login_form_helper'] = $this->_login_form_helper->getBlock($this->_data);
		$db = $this->_model->getDbConnection();
		if(!isset($this->_params)){
			$this->getResponse()->setRawHeader('HTTP/1.1 400 Bad Request')->sendResponse();
    		exit;
		}
		if(isset($this->_params['eID']) && isset($this->_params['ePass'])){
			
			$stMssege = $this->_model->validate($this->_params);//バリデート
			if($stMssege == ""){
				
				$emailData = $this->_model->CheckEmail($this->_params);
				
				if(isset($emailData)){
					$loginData = $this->_model->CheckPassword($this->_params);//ログイン
					if($loginData['M92_USER_CD']!=""){
						$this->_model->startTransaction($db);
						try{
							$this->_model->InsertLog($this->_params,$db);
							$this->_model->loginDate($loginData['M92_USER_CD'],$db);
							$this->_model->commitTransaction($db);
						}catch (Exception $e) {
							$this->_model->rollbackTransaction($db);
						}
						$db->closeConnection();
						if(!isset($e)){
						    $this->LoginOK();
						    exit;
						}
					}
				}
				//$this->_data['login_not_found_email_helper'] = $this->_login_not_found_email_helper->getBlock($this->_data);
        	}
        	//$this->_data['login']['mssege'] = $stMssege;
        	//$this->_data['login_error_helper'] = $this->_login_error_helper->getBlock($this->_data);
        	//$stMssege = "";
        }
        $this->LoginNg();
        exit;
	}
	
	/**
     * ログイン完了時に呼ばれる
     *
     * @access private
     * @return void
     */
	private function loginOk()
    {
    	Apm_Util::outputXmlHttpHeader();
        Apm_Util::outputXmlData('true', 'status');
        $this->setCookies();
        $allHeaderInfo = getallheaders();
        __log_debug(__FILE__, __LINE__, __METHOD__, '---------->>>');
        __log_debug_array(__FILE__, __LINE__, __METHOD__,$allHeaderInfo );
        __log_debug(__FILE__, __LINE__, __METHOD__, '----------<<<');
        exit;
    }
    
    /**
     * ログイン失敗時に呼ばれる
     *
     * @access private
     * @return void
     */
    private function loginNg()
    {
        Apm_Util::outputXmlHttpHeader();
        Apm_Util::outputXmlData('false', 'status');
        $this->setCookies();
        $allHeaderInfo = getallheaders();
        __log_debug(__FILE__, __LINE__, __METHOD__, '---------->>>');
        __log_debug_array(__FILE__, __LINE__, __METHOD__,$allHeaderInfo );
        __log_debug(__FILE__, __LINE__, __METHOD__, '----------<<<');
        exit;
    }
}
?>